<?php if(count($categories)>0):?>
<div class="top_categories">
    <?php $id_related = count($categories) > 4 ? ' id="top_categories"' : '';?>
    <ul<?php echo $id_related;?>>
    <?php foreach($categories as $category):?>
    <?php
    $image = is_null($category->url_images) ? 'default.jpg' : $category->url_images;
    $category_name = shorten_str($category->category, CATEGORY_NAME_MAXLENGTH);
    $uri = get_base_url() . url_title($category->category, 'dash', TRUE) . '-c' . $category->id;
    $active = $product_cat_id == $category->id ? ' active' : '';
    ?>
    <li class="related_item <?php echo $active;?>">
        <a href="<?php echo $uri; ?>">
            <img class="item_img" src="/images/categories/<?php echo $image; ?>" title="<?php echo $category->category; ?>" alt="<?php echo $category->category; ?>"/>
            <h4><?php echo $category_name;?></h4>
        </a>
    </li>
    <?php endforeach;?>
    <?php
        $count = count($categories);
        if($count < 5):
        for($i=0;$i<5-$count; $i++):
    ?>
    <li class="related_item">
        <a href="#">
            <img class="item_img" src="/images/categories/default.jpg"/>
            <h4 style="height: 10px;"></h4>
        </a>
    </li>
    <?php endfor; endif;?>
    </ul>
</div>
<?php endif;?>